package cn.kgc.vue.controller;

import cn.hutool.captcha.LineCaptcha;
import cn.hutool.core.util.IdUtil;
import cn.kgc.vue.commons.ResponseResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;

/**
 * @author 课工场
 * @date 2024/4/9
 * @description
 */
@RestController
@RequestMapping("/captcha")
@Api(tags = "验证码模块")
public class CaptchaController {

    @Resource
    private StringRedisTemplate stringRedisTemplate;

    @GetMapping
    @ApiOperation("生成验证码")
    public ResponseResult  createCaptcha(){

        LineCaptcha lineCaptcha = new LineCaptcha(110, 50, 4, 3);
        String code = lineCaptcha.getCode();
        // code 存入缓存 redis  uuid   3min有效
        String uuid = IdUtil.simpleUUID();
        stringRedisTemplate.opsForValue().set(uuid,code,3, TimeUnit.MINUTES);

        // 获取验证图片的base64 字符串 响应给前端
        String imageBase64 = lineCaptcha.getImageBase64();
        return ResponseResult.success().data("uuid",uuid).data("img",imageBase64);
    }

}
